package com.jm.springboot21.controller;

import com.jm.springboot21.pojo.Clazz;
import com.jm.springboot21.pojo.PageBean;
import com.jm.springboot21.pojo.Result;
import com.jm.springboot21.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/clazz")
@CrossOrigin("*")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    @GetMapping
    public Result findAll(){
        return Result.success(clazzService.findAll());
    }
    @GetMapping("/{name}")
    public Result findByName(@PathVariable String name){
        return Result.success(clazzService.findByName(name));
    }
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        clazzService.add(clazz);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        clazzService.deleteById(id);
        return Result.success();
    }
    @PutMapping
    public Result updateById(@RequestBody Clazz clazz){
        clazzService.updateById(clazz);
        return Result.success();
    }
    @GetMapping("/list")
    public Result listDepts(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "5") Integer pageSize) {

        PageBean pageData = clazzService.getDeptByPage(page, pageSize);
        return Result.success(pageData);
    }
    @GetMapping("/count")
    public Result count(){
        int count = clazzService.count();
        return Result.success(count);
    }
}
